/****************************************************************************
 *
 * Copyright (C) 2003-2009 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
/**
 * 
 */
package nfse.dataset.bluecoat;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.sql.*;
import java.util.Scanner;
import java.util.ArrayList;
import nfse.*;
import nfse.gui.record.*;

/**
 * @author bduphoff
 * 
 */
public class BlueCoatRecordTest extends NetFSERecordTest {

    private final String fileName = "data/bluecoat.txt";

    private final int numLogs = 94;

    private final int sensorID = 5;

    private BlueCoatRecordFactory factory;

    ArrayList<NetFSERecord> records = null;

    /**
     * @throws java.lang.Exception
     */
    @Before
    public void setUp() throws Exception {
        Scanner in = new Scanner(new File(fileName));
        while (in.hasNext()) {
            logs.add(in.nextLine());
        }
        in.close();
        factory = new BlueCoatRecordFactory();
    }

    /**
     * @throws java.lang.Exception
     */
    @After
    public void tearDown() throws Exception {

    }

    /**
     * Test method for
     * {@link nfse.dataset.lanl.NetScreen5400Record#getFlexRecord()}.
     */
    @Test
    public void testGetFlexRecord() {
        ArrayList<NetFSERecord> records = getRecords();
        for (NetFSERecord record : records) {
            int idx = -1;
            BlueCoatRecord temp = (BlueCoatRecord) record;
            FlexRecord fr = factory.getFlexRecord(temp);
            assertEquals(fr.sensorID, getSensorID());
            assertEquals(fr.values[++idx], temp.getStartTS().toString());
            assertEquals(fr.values[++idx], "" + temp.duration);
            assertEquals(fr.values[++idx], record.getSrcAddr());
        }
    }

    /**
     * Test method for
     * {@link nfse.dataset.lanl.NetScreen5400Record#getAlertMessage()}.
     */
    @Test
    public void testGetAlertMessage() {
        NetFSERecord rec = getRecord();
        assertTrue(rec.getAlertMessage() == null || rec.getAlertMessage().length() == 0);
    }

    /**
     * Test method for
     * {@link nfse.dataset.lanl.NetScreen5400Record#getFlexRecordFields()}.
     */
    @Test
    public void testGetFlexRecordFields() {
        String[] names = { "startts", "duration", "srcip", "dstip", "dsthost", "srcbytes", "dstbytes", "status",
                "action", "method", "uri", "useragent", "result", "category" };
        FlexRecordField[] fields = factory.getFlexRecordFields();
        assertEquals("Excpected " + names.length + " fields.", names.length, fields.length);
        for (int i = 0; i < fields.length; i++) {
            assertEquals(names[i], fields[i].columnName);
        }
    }

    /**
     * Test method for
     * {@link nfse.dataset.lanl.NetScreen5400Record#getIndexedFields()}.
     */
    @Test
    public void testGetIndexedFields() {
        int[] fields = getRecord().getIndexedFields();
        assertEquals("startts", NetFSE.getFieldName(fields[0]));
        assertEquals("srcip", NetFSE.getFieldName(fields[1]));
    }

    /**
     * Test method for {@link nfse.dataset.lanl.NetScreen5400Record#isAlert()}.
     */
    @Test
    public void testIsAlert() {
        assertFalse(factory.generate().isAlert());
    }

    /**
     * Test method for
     * {@link nfse.dataset.lanl.NetScreen5400Record#generate(java.lang.String, int)}
     * .
     */
    @Test
    public void testGenerateStringInt() {
        getRecords();
    }

    public ArrayList<NetFSERecord> getRecords() {
        records = new ArrayList<NetFSERecord>();
        int count = 0;
        for (String log : logs) {
            BlueCoatRecord temp = null;
            try {
                temp = (BlueCoatRecord) factory.generate(log, sensorID);
            } catch (Exception e) {
                e.printStackTrace();
                fail("Exception in parsing (" + e.getMessage() + ")'" + log + "'");
            }
            if (temp != null) {
                count++;
                records.add(temp);
            }
        }
        if (count != numLogs)
            fail("Expected " + numLogs + " but only parsed " + count + " from " + fileName);
        return records;
    }

    /**
     * Test attribute defaults
     */
    @Test
    public void testDefaults() {
        BlueCoatRecord rec = new BlueCoatRecord();

    }

    /**
     * Test method for {@link nfse.NetFSERecord#getEndTS()}.
     */
    @Test
    public void testGetEndTS() {
        ArrayList<NetFSERecord> records = getRecords();
        for (NetFSERecord record : records) {
            assertEquals(record.getStartTS(), record.getEndTS());
        }
    }

    /**
     * Test method for {@link nfse.NetFSERecord#setStartTS(java.sql.Timestamp)}.
     */
    @Test
    public void testStartTS() {
        NetFSERecord rec = getRecord();
        long ts = 100000000;
        Timestamp ts2 = new Timestamp(ts);
        rec.setStartTS(ts2);
        assertEquals(ts2, rec.getStartTS());
        assertEquals(ts2, rec.getEndTS());
        assertEquals(ts2, rec.getTimestamp());
    }

    @Override
    protected String getFileName() {
        return fileName;
    }

    @Override
    protected NetFSERecord getRecord() {
        if (records != null)
            return records.get(0);
        getRecords();
        return records.get(0);
    }

    @Override
    protected int getSensorID() {
        return sensorID;
    }

    /**
     * Test method for {@link nfse.NetFSERecord#isSyslog()}.
     */
    @Test
    public void testIsSyslog() {
        assertFalse(factory.isSyslog());
    }

    
}
